package jdbc;

import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Hello {
    public static void main(String[] args) throws SQLException {
        testInsert();
    }

    private static void testInsert() throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("666666");

        Connection connection = dataSource.getConnection();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号！");
        int id = scanner.nextInt();
        System.out.println("请输入姓名！");
        String name = scanner.next();

        String sql = "insert into student values (" + id + ",'" + name + "')";

        PreparedStatement statement = connection.prepareStatement(sql);

        int ret = statement.executeUpdate();
        System.out.println("ret= " + ret);

        statement.close();
        connection.close();
    }
}
